/****************************************************************

kernel/timeout.c
timeout event support for AWOS

Copyright (c) 2006 Gavin Wahl. All rights reserved.

Redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following
conditions are met:

1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided
with the distribution.

3. The end-user documentation included with the redistribution,
if any, must include the following acknowledgment:

"This product includes software developed by Andrew Wilcox and 
contributors for AWOS. (http://code.google.com/p/awos)."

Alternately, this acknowledgment may appear in the software
itself, if and wherever such third-party acknowledgments normally
appear.

4. The names "AWOS" and "Andrew Wilcox" must not be used to
endorse or promote products derived from this software without
prior written permission. For written permission, please contact
Andrew.R.Wilcox@Gmail.com.

5. Products derived from this software may not be called "AWOS"
 nor may "AWOS" appear in their name, without prior written
 permission of Andrew Wilcox.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ANDREW WILCOX BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.

****************************************************************/


#include <kernel.h>
#include <timeout.h>
#include <macros.h>


extern long long uptime;

struct timeout *timeouts[MAX_TIMEOUTS];
int num_timeouts;



void _insert_timeout(struct timeout *info)
{
	int i;
	int inserted=0;
	for(i=0; i<MAX_TIMEOUTS; ++i) {
		if(timeouts[i] == NULL) {
			timeouts[i]=info;
			inserted=1;
			break;
		}
	}
	assert(inserted);
	return;
}

void _timeout_init(void)
{
	int i;
	num_timeouts = 0;
	hz=100;
	for(i=0; i<MAX_TIMEOUTS; ++i)
		timeouts[i] = NULL;

}

void timeout_set(struct timeout *info, void (*callback)(void *), void *arg)
{
	info->callback = callback;
	info->arg = arg; 
	info->flags = TIMEOUT_INIT;
}

void timeout_add(struct timeout *info, unsigned time)
{
	assert(num_timeouts < MAX_TIMEOUTS);
	num_timeouts++;
	assert(info->flags & TIMEOUT_INIT);
	info->time = time + uptime;
	info->flags |= TIMEOUT_PENDING;
	_insert_timeout(info);
}
	
void timeout_del(struct timeout *info)
{
	int i;
	int found=0;
	for(i=0; i<MAX_TIMEOUTS; ++i) {
		if(timeouts[i] == info) {
			info->flags &= ~TIMEOUT_PENDING;
			timeouts[i] = NULL;
			found = 1;
			num_timeouts--;
			break;
		}
	}
	assert(found);
	return;
}



void _exec_timeout(struct timeout *info)
{
	assert(info->flags & TIMEOUT_PENDING);
	timeout_del(info);
	(info->callback)(info->arg);
	info->flags &= ~TIMEOUT_PENDING;
	info->flags |= TIMEOUT_RAN;
}
	





/***
  this might be a canidate for coding in asm 
   because it has to run every tick
***/

void _do_timeouts(unsigned long long uptime)
{
	int i;
	for(i=0; i<MAX_TIMEOUTS; ++i) {
		if(timeouts[i] && timeouts[i]->time == uptime)
			_exec_timeout(timeouts[i]);
	}
}

